//
//  ReachabilityTool.swift
//  YingXunTong
//
//  Created by 孙先华 on 17/6/15.
//  Copyright © 2017年 سچچچچچچ. All rights reserved.
//

enum reachabilityType {
    case WiFiType
    case WAAFType
    case NoReachabilityType  //无连接
}
import Foundation
import Reachability

public class ReachabilityTool: NSObject {
    
    let reachability = try! Reachability.init()
    
    func NetworkStatusListener() {
        // 1、设置网络状态消息监听 2、获得网络Reachability对象
        NotificationCenter.default.addObserver(self, selector: #selector(self.reachabilityChanged(note:)),name: Notification.Name.reachabilityChanged,object: reachability)
        do{
            // 3、开启网络状态消息监听
            try reachability.startNotifier()
        }catch{
            print("could not start reachability notifier")
        }
    }
    
    // 移除消息通知
    deinit {
        
        // 关闭网络状态消息监听
        reachability.stopNotifier()
        // 移除网络状态消息通知
        NotificationCenter.default.removeObserver(self, name: Notification.Name.reachabilityChanged, object: reachability)
    }
    
    // 主动检测网络状态
    @objc func reachabilityChanged(note: NSNotification){
        let reachability = note.object as! Reachability // 准备获取网络连接信息
    }
    
    func checkReachbleState() ->reachabilityType{

        if reachability.isReachable() { // 判断网络连接状态
            print("网络连接：可用")
            if reachability.isReachableViaWiFi() { // 判断网络连接类型
                return.WiFiType
            } else {
                print("连接类型：移动网络")
                return .WAAFType
            }
        } else {
            print("网络连接：不可用")
            return .NoReachabilityType
        }
    }
    
}


